package com.tvunetworks.center.user.controller;

import com.tvunetworks.center.common.constant.LocalConstant;
import com.tvunetworks.center.common.model.LoginUser;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.center.user.annotation.Logined;
import com.tvunetworks.center.user.model.param.AddRoleManagementParam;
import com.tvunetworks.center.user.model.param.PageRoleParam;
import com.tvunetworks.center.user.model.param.UpdateRoleManagementParam;
import com.tvunetworks.center.user.model.param.SearchRoleUserParam;
import com.tvunetworks.center.user.model.result.ResultConstant;
import com.tvunetworks.center.user.service.RoleManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author: Etan Yang
 * @data: 2021/4/7 15:06
 * @program: tvucc-aggregator
 * @description:
 */

@RestController
@RequestMapping("/roleManagement")
class RoleManagementController {

    @Autowired
    private RoleManagementService roleManagementService;

    /**
     * 添加role
     * @param loginUser
     * @param param
     * @return
     */
    @PostMapping("add")
    public Result<Object> addRoleManagement(@Logined LoginUser loginUser, @Valid @RequestBody AddRoleManagementParam param) {
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        return roleManagementService.addRoleManagement(loginUser, param);
    }

    /**
     * 根据userId分页查询Role
     * @param loginUser
     * @param param
     * @return
     */
    @PostMapping("page")
    public Result<Object> pageRole(@Logined LoginUser loginUser, @RequestBody PageRoleParam param) {
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        if (!LocalConstant.UserRole.ADMIN.equalsIgnoreCase(loginUser.getRole())) {
            return ResultConstant.RoleManagement.ADMIN_PERMISSION;
        }
        return roleManagementService.pageRole(loginUser.getId(), param);
    }

    /**
     * 修改Role
     * @param loginUser
     * @param param
     * @return
     */
    @PostMapping("update")
    public Result<Object> updateRole(@Logined LoginUser loginUser, @Valid @RequestBody UpdateRoleManagementParam param) {
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        return roleManagementService.updateRole(loginUser, param);
    }

    /**
     * 根据roleId删除role
     * @param loginUser
     * @param roleId
     * @return
     */
    @PostMapping("delete")
    public Result<Object> deleteRole(@Logined LoginUser loginUser, @RequestParam("roleId") String roleId) {
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        return roleManagementService.deleteRole(loginUser, roleId);
    }

    /**
     * 分页查询roleName
     * @param loginUser
     * @param param
     * @return
     */
    @PostMapping("pageRoleName")
    public Result<Object> pageRoleName(@Logined LoginUser loginUser, @RequestBody PageRoleParam param) {
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        return roleManagementService.pageRoleName(loginUser.getId(), param);
    }

    /**
     * 根据roleId查询该role拥有的feature和device
     * @param loginUser
     * @param param
     * @return
     */
    @PostMapping("getRoleMessage")
    public Result<Object> getRoleMessage(@Logined LoginUser loginUser, @RequestBody SearchRoleUserParam param) {
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        return roleManagementService.getRoleMessage(loginUser, param);
    }


    /**
     * 根据用户查询该用户的所有Receiver
     * @param loginUser
     * @return
     */
    @PostMapping("getReceiverList")
    public Result<Object> getReceiverListByUserId(@Logined LoginUser loginUser,@RequestBody SearchRoleUserParam param) {
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        return ResultUtil.success(roleManagementService.getReceiverList(loginUser.getId(),param));
    }
}
